<template>
  <ContainerGroup>
    <Container style="margin-bottom: 10px">
      <FilterForm
        ref="filterForm"
        :elements="filterElements"
        @submit="resetForm"
        @reset="searchSubmit"
      >
      <Button style="display: inline-block;margin-right:10px;" @click="handleCheck">检查</Button>
      <Button
          style="display: inline-block;"
          @click="handleExport"
          >导出</Button
        >
      </FilterForm>
    </Container>
    <Container height="fill" :scroll-y="true" display="flexY">
      <OperationContainer style="margin-bottom: 10px">
        <Button icon="add" @click="handleAdd">新增</Button>
      </OperationContainer>
      <el-table
        v-loading="tableLoading"
        :data="tableData"
        border
        :header-cell-style="{ background: '#fafafa', borderColor: '#e7ecf8' }"
        header-row-class-name="ck-table-header"
        row-class-name="ck-table-row"
        size="small"
        height="100%"
      >
        <el-table-column align="center" type="index" label="" width="60" />
        <el-table-column prop="edExtraFirstCode" label="HIS科室编码" />
        <el-table-column prop="edExtraFirstDescription" label="HIS科室名称" />
        <el-table-column prop="edExtraSecondCode" label="成本科室编号" />
        <el-table-column prop="edExtraSecondDescription" label="成本科室名称" />
        <el-table-column align="center" label="是否启用" width="120">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.rowStatus"
              active-color="#FFA712"
              active-value="0"
              inactive-value="1"
              @change="switchUse(scope.row)"
            />
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作" width="120">
          <template slot-scope="scope">
            <!-- <el-tooltip
              class="item"
              effect="dark"
              content="编辑"
              placement="top"
            >
              <i
                class="el-icon-edit-outline"
                style="
                  padding-top: 4px;
                  font-size: 20px;
                  color: #207efa;
                  cursor: pointer;
                "
                @click="handleEdit(scope.row)"
              />
            </el-tooltip> -->
            <el-tooltip
              class="item"
              effect="dark"
              content="删除"
              placement="top"
              style="margin-left: 10px"
            >
              <i
                class="el-icon-delete"
                style="
                  padding-top: 4px;
                  font-size: 20px;
                  color: #207efa;
                  cursor: pointer;
                "
                @click="handleDel(scope.row)"
              />
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <Pagination
        :total="pageAttr.total"
        :page="pageAttr.currentPage"
        :page-size="pageAttr.pageSize"
        @pagation="pagationChange"
      />
      <Dialog
        :title="title"
        :visible="dialogVisible"
        :footer-btns="[
          {
            text: '保存',
            type: 'primary',
            trigger: 'confirm',
          },
          {
            text: '取消',
            type: 'info',
            trigger: 'cancel',
          },
        ]"
        @cancel="dialogVisible = false"
        @confirm="handleConfirm"
      >
        <el-form ref="form" :model="formInfo" label-width="180px" size="small">
          <el-form-item label="HIS科室名称">
            <el-select
              v-model="formInfo.edExtraFirstCode"
              placeholder="请选择"
              :disabled="title == '编辑'"
              filterable
            >
              <el-option
                v-for="item in gzDept"
                :key="item.deptCode"
                :label="item.deptName"
                :value="item.deptCode"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="成本科室名称">
            <el-select
              v-model="formInfo.edExtraSecondCode"
              placeholder="请选择"
              filterable
              multiple
            >
              <el-option
                v-for="item in hsDYList"
                :key="item.deptCode"
                :label="item.deptName"
                :value="item.deptCode"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </Dialog>
      <Dialog
        title="提示"
        :visible="tableDialog"
        :footer-btns="[
          {
            text: '取消',
            type: 'info',
            trigger: 'cancel',
          },
        ]"
        @cancel="tableDialog = false"
      >
        <el-table
          v-loading="isDialog"
          :data="tableDataDialog"
          border
          :header-cell-style="{ background: '#fafafa', borderColor: '#e7ecf8' }"
          header-row-class-name="ck-table-header"
          row-class-name="ck-table-row"
          size="small"
          height="40vh"
          style="width: 450px"
        >
          <el-table-column align="center" type="index" label="" width="60" />
          <el-table-column
            prop="deptCode"
            label="HIS科室代码"
            width="120"
          />
          <el-table-column prop="deptName" label="HIS科室名称" />
        </el-table>
      </Dialog>
    </Container>
  </ContainerGroup>
</template>

<script>
import {
  getEnumDetailSourcePage,
  batchDeleteEnumDetail,
  DeptHisRelation,
  addEnumDetail,
  modifyEnumDetail,
  getEnumDetailSource
} from "../../../utils/apis/basic";

import { updateHisStatusByEdId,listEnumDetailIsLinkByFuzzy,insertEnumDetailWithSalary} from "../../../utils/apis/dataExchange/index";

import {
  getSalaryRelation,
  getDeptList,
  getNameList,
  setSalaryRelation,
} from "../../../utils/apis/dataConversionRules";
import {
  getDepartmentSource,
  listEnumDetailUnLink,
  listEtlDepartmentFuzzy
} from "../../../utils/apis/accountingUnit/share.js";
import { BasicParams } from "../../../utils/basicParams";
import { Force } from "../../../utils/force";
export default {
  name: "HIS", // 科室-HIS科室
  components: {},
  data() {
    return {
      tableDialog: false,
      isDialog: false,
      tableDataDialog: [],
      gzDept: [],
      hsDYList: [],
      formInfo: {
        edExtraFirstCode: "",
        edExtraSecondCode: "",
      },
      title: "",
      dialogVisible: false,
      pageAttr: {
        pageSize: 20,
        currentPage: 1,
        total: 0,
      },
      filterElements: [
        {
          key: "edExtraFirstCode",
          type: "Select",
          label: "HIS科室名称",
          value: "",
          width: 210,
          itemLabel: "deptName",
          itemValue: "deptCode",
          options: [],
          filterable: true,
          clearable: true,
        },
        {
          key: "edExtraSecondCode",
          type: "Select",
          label: "成本科室名称",
          value: "",
          width: 210,
          itemLabel: "deptName",
          itemValue: "deptCode",
          options: [],
          filterable: true,
          clearable: true,
        },
        {
          key: "relateCode",
          type: "Select",
          label: "关联状态",
          value: "",
          width: 210,
          options: [
            {
              label: "全部",
              value: "",
            },
            {
              label: "已关联",
              value: "1",
            },
            {
              label: "未关联",
              value: "0",
            },
          ],
          filterable: true,
          clearable: true,
        },
        {
          key: "rowStatus",
          type: "Select",
          label: "启用状态",
          value: "",
          width: 210,
          options: [
            {
              label: "全部",
              value: "",
            },
            {
              label: "已启用",
              value: "0",
            },
            {
              label: "未启用",
              value: "1",
            },
          ],
          filterable: true,
          clearable: true,
        },
      ],
      tableData: [],
      tableLoading: false,
      newTime: "",
      rowId: "",
      newParamList: {},
    };
  },
  created() {},
  mounted() {
    this.getSelect();
    this.getHsSelect();
  },
  methods: {
    handleCheck(value) {
      let basicParams = new BasicParams();
        basicParams.edParentCode = "RD_DeptHisRelation";
        basicParams.sysId = "HIS";
        basicParams.relationStatus = 0;
        basicParams.isLeaf=null;
      listEnumDetailUnLink(basicParams).then((res) => {
        if (res.data.code == 200) {
          this.tableDialog = true;
          this.tableDataDialog = res.data.data;
        }
      });
    },
       handleExport(){ //导出
    DeptHisRelation().then(res => {
        const blob = new Blob([res.data], {
          type: res.data.type + 'charset=utf-8'
        })
        const downloadLink = document.createElement('a')
        const URL = window.URL || window.webkitURL || window.moxURL
        const downUrl = URL.createObjectURL(blob)
        downloadLink.href = downUrl
        downloadLink.download = '科室-HIS科室.xlsx'
        downloadLink.click()
        window.URL.revokeObjectURL(downUrl)
        // document.body.removeChild(downloadLink) // 删除 a 标签
      })
    },
    handleEdit(row) {
      // this.formInfo.edExtraFirstCode = "";
      // this.formInfo.edExtraSecondCode = "";
      // this.rowId = row.edId;
      // this.title = "编辑";
      // this.dialogVisible = true;
      // console.log("row.edId", row.edId);
      // getEnumDetailWithSalary(row.edId).then((res) => {
      //   if (res.data.code == 200) {
      //     this.formInfo.edExtraFirstCode = res.data.data.edExtraFirstCode;
      //     this.formInfo.edExtraSecondCode = res.data.data.edExtraSecondCode;
      //   }
      // });



      this.rowId = row.edId;
      this.title = "编辑";
      this.dialogVisible = true;
      let basicParams = new BasicParams();
      basicParams.edId = row.edId;
      basicParams.edParentCode = "RD_DeptHisRelation";
      this.formInfo.edExtraFirstCode = "";
      this.formInfo.edExtraSecondCode = "";
      if (row.edId) {
        getEnumDetailSource(basicParams).then((res) => {
          if (res.data.code == 200) {
            this.formInfo.edExtraFirstCode = res.data.data[0].edExtraFirstCode;
            this.formInfo.edExtraSecondCode =
              res.data.data[0].edExtraSecondCode;
          }
        });
      } else {
        this.formInfo.edExtraFirstCode = row.edExtraFirstCode;
      }

    },
    handleDel(row) {
      //删除
      let ids = [row.edId];
      this.$confirm({
        title: "温馨提示",
        content: "您确定要删除该条数据?",
        okText: "确定",
        cancelText: "取消",
        okFn: () => {
          batchDeleteEnumDetail(ids).then((res) => {
            if (res.data.code === 200) {
              this.$message.success(res.data.message);
              this.getData();
            } else {
              this.$message.error(res.data.message);
            }
          });
        },
        cancelFn: () => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        },
      });
    },
    handleConfirm() {
      //确定
      if (!this.formInfo.edExtraFirstCode) {
        this.$message.warning("请选择HIS科室名称");
        return;
      }
      if (!this.formInfo.edExtraSecondCode) {
        this.$message.warning("请选择成本科室名称");
        return;
      }

      let basicParams = new BasicParams();
      basicParams.edExtraFirstCode = this.formInfo.edExtraFirstCode;
      basicParams.edExtraFirstDescription = this.gzDept.find(
        (item) => this.formInfo.edExtraFirstCode == item.deptCode
      ).deptName;
      basicParams.edExtraSecondCode = this.formInfo.edExtraSecondCode;

      basicParams.edParentCode = "RD_DeptHisRelation";
      basicParams.edName = `科室-HIS对应`;


      if (this.title == "新增") {
        addEnumDetail(basicParams).then((res) => {
          if (res.data.code === 200) {
            this.$message.success(res.data.message);
            this.dialogVisible = false;
            this.getData();
          } else {
            this.$message.error(res.data.message);
          }
        });
      } else {
        basicParams.edId = this.rowId;
        modifyEnumDetail(basicParams).then((res) => {
          if (res.data.code === 200) {
            this.$message.success(res.data.message);
            this.dialogVisible = false;
            this.getData();
          } else {
            this.$message.error(res.data.message);
          }
        });
      }
    },
    handleAdd() {
      //新增
      this.getSelect(0); //未关联
      this.formInfo.edExtraFirstCode = "";
      this.formInfo.edExtraSecondCode = "";
      this.title = "新增";
      this.dialogVisible = true;
    },
    // 分页
    pagationChange(payload) {
      this.pageAttr.currentPage = payload.page;
      this.pageAttr.pageSize = payload.pageSize;
      this.getData(this.newParamList);
    },
    getData(val) {
      // 获取列表
      let basicParams = new BasicParams();
      if (val) {
        basicParams.edExtraFirstCode = val.edExtraFirstCode; // 工资科室编码
        basicParams.edExtraSecondCode = val.edExtraSecondCode; // 核算单元编码
        basicParams.relationStatus = val.relateCode; // 关联状态编码日 1 关联 0 未关联
        basicParams.rowStatus = val.rowStatus; // 启用状态编码日 0 启用 1 未启用
      }
      basicParams.sysId = "HIS";
      basicParams.edParentCode = "RD_DeptHisRelation";
      basicParams.isLeaf = null
      basicParams.pageIndex = this.pageAttr.currentPage;
      basicParams.pageSize = this.pageAttr.pageSize;
      this.tableLoading = true;
      listEnumDetailIsLinkByFuzzy(basicParams).then((res) => {
        if (res.data.code === 200) {
          this.tableLoading = false;
          this.tableData = res.data.data.list;
          this.pageAttr.total = res.data.data.total || 0;
        }
      });
    },
    getHsSelect() {
      // 核算单元
      getDepartmentSource({
        projectId: "1",
        SysId: "Cost",
      }).then(async (res) => {
        if (res.data.code === 200) {
          this.filterElements[1].options = res.data.data;
          this.hsDYList = res.data.data;
        }
      });
    },
    getSelect(status) {
      if (status == 0) {
        let basicParams = new BasicParams();
        basicParams.edParentCode = "RD_DeptHisRelation";
        basicParams.sysId = "HIS";
        basicParams.relationStatus = 0;
        basicParams.isLeaf=null;
        listEnumDetailUnLink(basicParams).then((res) => {
          if (res.data.code === 200) {
            this.gzDept = res.data.data;
          }
        });
      } else {
        // 个人工资（人事科室）
        listEtlDepartmentFuzzy({
          projectId: "1",
          sysId: "HIS",
          relationStatus: status,
        }).then((res) => {
          if (res.data.code === 200) {
            this.filterElements[0].options = res.data.data;
          }
        });
      }
    },
    resetForm(data) {
      // 查询
      this.newParamList = data;
      this.getData(data);
    },
    switchUse(row) {
      // let force = new Force();
      // force.edId = row.edId;
      // force.rowStatus = row.rowStatus;
      // updateHisStatusByEdId(force).then((res) => {
      //   if (res.data.code === 200) {
      //     this.$message.success(res.data.message);
      //     this.getData();
      //   } else {
      //     this.$message.error(res.data.message);
      //   }
      // });

       const basicParams = new BasicParams()
      basicParams.edId = row.edId
      basicParams.rowStatus = row.rowStatus
      basicParams.edParentCode = 'RD_DeptHisRelation'
      modifyEnumDetail(basicParams).then((res) => {
        if (res.data.code === 200) {
          this.$message.success(res.data.message)
          this.getData()
        } else {
          this.$message.error(res.data.message)
        }
      })
    },
    searchSubmit() {}, // 重置
  },
};
</script>

<style lang="less" scoped>
.el-table--mini,
.el-table--small,
.el-table__expand-icon {
  font-size: 14px !important;
}

.ck-table-header {
  th {
    background: #fafafa !important;
    height: 48px;
    color: #555555 !important;
  }
}
.pagenation-box {
  display: flex;
  padding-top: 2px;
  justify-content: flex-end;
}
</style>
